﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件名：CommendManage.cs
  //文件功能描述：推荐管理
  //创建人：赵国伟
  //生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class CommendManage
    {

        private readonly DALGoodsCommend dalCommend = new DALGoodsCommend();

        #region "分页查询商品推荐信息； by 赵国伟 2012-04-19"
        /// <summary>
        /// 分页查询商品推荐信息；
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public List<GoodsCommendInfo> QueryAsPage(QueryCommendAsPage q)
        {
            string cond = "1=1 ";
            if (!string.IsNullOrEmpty(q.KeyWords))
                cond += " and GoodsID in(Select GoodsID From Goods Where GoodsName like '%" + q.KeyWords + "%') ";
            if (q.CategoryID > 0)
                cond += " and CategoryID=" + q.CategoryID;
            List<GoodsCommend> source = dalCommend.QueryListAsPage(cond, "OrderValue,CommendDate desc", q.CurPage, q.PageSize, out q.TotalCount);
            List<GoodsCommendInfo> target = null;
            if (source != null)
            {
                target = new List<GoodsCommendInfo>();
                Dictionary<int, string> dictCategory = DictionariesManage.GetDictionariesToHash(DictionaryCategory.商品推荐类别);
                Dictionary<int, string> dictGoodsName = BLLHelper.GenerateToHash<Goods>("GoodsID in(Select GoodsID From GoodsCommend Where " + cond + ")", GoodsEnum.GoodsName);
                foreach (GoodsCommend s in source)
                {
                    GoodsCommendInfo t = new GoodsCommendInfo();
                    t.CommendInfo = s;
                    t.CategoryName = dictCategory[s.CategoryID];
                    t.GoodsName = dictGoodsName[s.GoodsID];
                    target.Add(t);
                }
            }
            return target;
        }
        #endregion

        #region "删除指定的推荐； by 赵国伟 2012-04-19"
        /// <summary>
        /// 删除指定的推荐；
        /// </summary>
        /// <param name="GoodsCommendID"></param>
        /// <returns></returns>
        public ReturnValue Delete(int GoodsCommendID)
        {
            ReturnValue ret = new ReturnValue();
            if (dalCommend.Delete(GoodsCommendID) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "删除成功！";
            }
            return ret;
        }
        #endregion

        #region "保存一条商品推荐信息 by 赵国伟 2012-04-19"
        /// <summary>
        /// 保存一条商品推荐信息
        /// </summary>
        /// <returns></returns>
        public ReturnValue Save(GoodsCommend c)
        {
            ReturnValue ret = new ReturnValue();
            c.CommendDate = DateTime.Now;

            //判断是否在同一栏目下重复推荐；
            if (dalCommend.QueryList(1, "GoodsID=" + c.GoodsID + " and CategoryID=" + c.CategoryID + " and GoodsCommendID<>" + c.GoodsCommendID, "") != null)
            {
                ret.Message = "该栏目下重复推荐此商品，请重新选择商品！";
                return ret;
            }

            FilesManage.FormatCommendImg(c);

            if (c.GoodsCommendID <= 0)
            {
                if (dalCommend.Add(c) > 0)
                {
                    ret.Result = ResultType.Success;
                    ret.Message = "保存成功！";
                }
            }
            else
            {
                GoodsCommend o = dalCommend.GetModel(c.GoodsCommendID);
                c.CommendDate = o.CommendDate;
                if (dalCommend.Update(c) > 0)
                {
                    ret.Result = ResultType.Success;
                    ret.Message = "保存成功！";
                }
            }
            return ret;
        }
        #endregion

        #region "查询一条指定的商品推荐信息； by 赵国伟 2012-04-19"
        /// <summary>
        /// 查询一条指定的商品推荐信息；
        /// </summary>
        /// <param name="GoodsCommendID"></param>
        /// <returns></returns>
        public GoodsCommendInfo GetCommend(int GoodsCommendID)
        {
            GoodsCommend s = dalCommend.GetModel(GoodsCommendID);
            GoodsCommendInfo t = null;
            if (s != null)
            {
                t = new GoodsCommendInfo();
                t.CommendInfo = s;
                t.GoodsName = DALHelper.GetFieldValue<Goods>(s.GoodsID, GoodsEnum.GoodsName.ToString());
                t.CategoryName = DALHelper.GetFieldValue<Dictionaries>(s.CategoryID, DictionariesEnum.DictionaryName.ToString());
            }
            return t;
        }
        #endregion


        #region "获取指定条件下推荐的抢购商品； by 赵国伟 2012-08-11"
        /// <summary>
        /// 获取指定条件下推荐的抢购商品；
        /// </summary>
        /// <param name="top"></param>
        /// <param name="categoryId"></param>
        /// <returns></returns>
        public DataTable GetRushCommends(int top, int categoryId)
        {
            return dalCommend.GetRushCommends(top, categoryId);

        }
        #endregion



    }
}
